<template>
  <div class="app-container">
    <el-row :gutter="20" class="mb8" type="flex" justify="space-between">
      <el-col :span="1.5">
        <el-button round icon="el-icon-back" @click="goBack">返回</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" @click="submitForm">保存</el-button>
        <el-button @click="reset">重置</el-button>
      </el-col>
    </el-row>
    <el-divider></el-divider>
    <el-row :gutter="50">
      <el-col :span="8">
        <el-form ref="form" :model="form" :rules="rules" label-width="120px">
          <el-form-item label="助记符" prop="spell">
            <el-input
              v-model="form.spell"
              placeholder="请输入"
              style="width: 100%"
            />
          </el-form-item>

          <el-form-item label="名称" prop="name">
            <el-input
              v-model="form.name"
              placeholder="请输入"
              style="width: 100%"
            />
          </el-form-item>

          <el-form-item label="数据类型" prop="dataType">
            <el-select
              v-model="form.dataType"
              placeholder="请选择"
              style="width: 100%"
              filterable
              clearable
            >
              <el-option
                v-for="dict in [
                  {
                    value: 1,
                    label: '数字',
                  },
                  {
                    value: 2,
                    label: '文字',
                  },
                ]"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="项目类型" prop="category">
            <el-select
              v-model="form.category"
              placeholder="请选择"
              style="width: 100%"
              filterable
              clearable
            >
              <el-option
                v-for="dict in [
                  {
                    value: 1,
                    label: '值',
                  },
                  {
                    value: 2,
                    label: '值',
                  },
                ]"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="上限值" prop="rangMax">
            <el-input
              v-model="form.rangMax"
              placeholder="请输入"
              style="width: 100%"
            />
          </el-form-item>

          <el-form-item label="下限值" prop="rangMin">
            <el-input
              v-model="form.rangMin"
              placeholder="请输入"
              style="width: 100%"
            />
          </el-form-item>

          <el-form-item label="单位" prop="permittedUnit">
            <el-input
              v-model="form.permittedUnit"
              placeholder="请输入"
              style="width: 100%"
            />
          </el-form-item>

          <el-form-item label="顺序" prop="displayOrder">
            <el-input
              v-model="form.displayOrder"
              placeholder="请输入"
              style="width: 100%"
            />
          </el-form-item>

          <el-form-item label="备注" prop="description">
            <el-input
              v-model="form.description"
              type="textarea"
              placeholder="请输入"
              show-word-limit
              maxlength="200"
              :rows="4"
            />
          </el-form-item>
        </el-form>
      </el-col>
      <el-col :span="16">
        <el-row class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              >新增</el-button
            >
          </el-col>
        </el-row>
        <el-table v-loading="loading" :data="detailItems" highlight-current-row>
          <el-table-column label="常用值" align="center" prop="normalCode">
            <template slot-scope="{ row }">
              <el-input
                v-model="row.normalCode"
                class="edit-input"
                size="small"
              />
            </template>
          </el-table-column>
          <el-table-column label="顺序" align="center" prop="displayOrder">
            <template slot-scope="{ row }">
              <el-input
                v-model="row.displayOrder"
                class="edit-input"
                size="small"
              />
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { getItem, updateItem, addItem, deleteItem } from "@/api/detail";
export default {
  name: "form",

  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        skipCount: 0,
        maxResultCount: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        // name: [{ required: true, message: "名称不能为空", trigger: "blur" }],
        // spell: [{ required: true, message: "助记符不能为空", trigger: "blur" }],
        // dataType: [
        //   { required: true, message: "数据类型不能为空", trigger: "blur" },
        // ],
        // category: [
        //   { required: true, message: "项目类型不能为空", trigger: "blur" },
        // ],
        // rangMax: [
        //   { required: true, message: "上限值不能为空", trigger: "blur" },
        // ],
        // rangMin: [
        //   { required: true, message: "下限值不能为空", trigger: "blur" },
        // ],
        // permittedUnit: [
        //   { required: true, message: "单位不能为空", trigger: "blur" },
        // ],
        // displayOrder: [
        //   { required: true, message: "顺序不能为空", trigger: "blur" },
        // ],
        // description: [
        //   { required: true, message: "备注不能为空", trigger: "blur" },
        // ],
      },
      // 常用取值
      detailItems: [],
    };
  },
  methods: {
    getItems() {
      // const id = row.id || this.ids;
      // getItem(id).then((response) => {
      //   this.form = response.data;
      //   this.$nextTick(() => {
      //     this.form = response.data;
      //   });
      //   this.open = true;
      //   this.title = "编辑";
      // });
    },
    // 返回
    goBack() {
      this.$router.back();
    },
    // 新增一列
    handleAdd() {
      this.detailItems.unshift({});
    },
    /** 提交按钮 */
    submitForm() {
      console.log("this.form", this.form);
      console.log("this.detailItems", this.detailItems);
      this.$refs["form"].validate((valid) => {
        if (valid) {
          let form = {
            ...this.form,
            detailItems: this.detailItems,
          };
          if (form.id != null) {
            updateItem(form).then((response) => {
              this.$modal.msgSuccess("编辑成功");
              this.open = false;
              this.getList();
            });
          } else {
            addItem(form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    // 取消按钮
    cancel() {
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        spell: undefined,
        dataType: undefined,
        category: undefined,
        displayOrder: undefined,
        detailItems: [],
      };
      this.detailItems = [];
      this.resetForm("form");
    },
  },
};
</script>

<style>
</style>
